package dyy.zc.sportruler.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.base.BaseActivity;
import dyy.zc.sportruler.bean.UserInfo;
import dyy.zc.sportruler.utils.ImageLoaderUtils;
import dyy.zc.sportruler.views.DragScaleImageView;


/**
 * Created by Administrator on 2016/7/26 0026.
 */
public class MyHomeActivity extends BaseActivity implements View.OnClickListener {
    public int preHeight;//拉伸之前的高度
    public int width;//设置旋转图片的宽度
    private DragScaleImageView mDragScaleImageView;//可拉伸放大的背景图片
    private ImageView head, sex;//头像 性别
    private TextView name, phone, age, height, weight, jiaoling,content;//昵称 电话 年龄 身高 体重 教龄
    private RelativeLayout rl_jianjie;
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    protected void findViewByIDS() {
        head = findViewsById(R.id.activity_mylhome_iv_touxiang);
        name = findViewsById(R.id.activity_personalhome_tv_name);
        sex = findViewsById(R.id.activity_personalhome_iv_man);
        phone = findViewsById(R.id.textView2);
        age = findViewsById(R.id.activity_personalhome_tv_age);
        height = findViewsById(R.id.activity_personalhome_tv_tall);
        weight = findViewsById(R.id.activity_personalhome_tv_weight);
        jiaoling = findViewsById(R.id.activity_personalhome_tv_teachyear);
        mDragScaleImageView = (DragScaleImageView) findViewById(R.id.rl_head);
        rl_jianjie = findViewsById(R.id.jianjie);
        content = findViewsById(R.id.content);
        initDragScaleImageView();
    }

    private void setTransparentBar() {
        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTransparentBar();
        setContentView(R.layout.activity_myhome);
        initData(loginuser);
        bind();
    }

    private void bind() {
        rl_jianjie.setOnClickListener(this);
    }


    /**
     * 初始化拉伸图片
     */
    public void initDragScaleImageView() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        int height = metric.heightPixels;   // 屏幕高度（像素）
        preHeight = dip2px(this, 223);
        mDragScaleImageView.setImageWidthAndHeight(width, preHeight);
    }



    /**
     * 初始化个人信息
     */
    private void initData(UserInfo userInfo) {
        ImageLoaderUtils.initImage(this, userInfo.getHead().getFileUrl(), head, R.mipmap.logo);
        name.setText(userInfo.getName());
        if (userInfo.getSex().equals("男")) {
            sex.setImageResource(R.mipmap.man);
        } else {
            sex.setImageResource(R.mipmap.girl);
        }
        phone.setText(userInfo.getAccount());
        age.setText(userInfo.getAge());
        height.setText(userInfo.getHeight());
        weight.setText(userInfo.getWeight());
        jiaoling.setText(userInfo.getJiaoling());
        if (userInfo.getContent()!=null){
            if (!userInfo.getContent().equals(""))
                content.setText(userInfo.getContent());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.jianjie:
                Intent intent = new Intent(MyHomeActivity.this,JianjieActivity.class);
                startActivityForResult(intent,123);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 123 :
                if (data != null){
                    UserInfo userInfo = (UserInfo) data.getSerializableExtra("login");
                    initData(userInfo);
                }
                break;
        }
    }
}
